package com.peanut.service.impl;

import com.orm.mapper.BaseService;
import com.peanut.entity.Game;
import com.peanut.entity.GameType;
import com.peanut.entity.Page;
import com.peanut.entity.vo.BookVO;
import com.peanut.mapper.GameMapper;
import com.peanut.mapper.GameTypeMapper;
import com.peanut.service.GameService;
import com.spring.annotation.Autowired;
import com.spring.annotation.Service;

import java.util.List;

@Service
public class GameServiceImpl extends BaseService<GameMapper,Game>  implements GameService {
	
	@Autowired
	private GameTypeMapper gameTypeMapper;
	
	
	@Override
	public List<GameType> queryGameType() {
		return gameTypeMapper.selectAll();
	}
	
	@Override
	public Page<Game> queryNormalGame(Integer currentPage,Integer pageSize) {
		int total = baseMapper.count();
		int pageCount = (int) (Math.ceil((double) total / pageSize));     // 计算分页数量和当前处于第几页
		currentPage = currentPage > pageCount ? pageCount : currentPage;
		
		Page<Game> listPage = new Page<>();
		listPage.setTotal(total);
		listPage.setPageCount(pageCount);
		listPage.setPageSize(pageSize);
		listPage.setCurrentPage(currentPage);

		if(total != 0){
			listPage.setData(baseMapper.queryNormalGame(pageSize*(currentPage-1),pageSize));
		}
		
		return listPage;
	}
	
	@Override
	public Page<Game> queryNormalGameByPlatform(Integer currentPage, Integer pageSize, Integer platform) {
		int total = baseMapper.countNormalGameByRectType(platform);
		int pageCount = (int) (Math.ceil((double) total / pageSize));     // 计算分页数量和当前处于第几页
		currentPage = currentPage > pageCount ? pageCount : currentPage;
		
		Page<Game> listPage = new Page<>();
		listPage.setTotal(total);
		listPage.setPageCount(pageCount);
		listPage.setPageSize(pageSize);
		listPage.setCurrentPage(currentPage);
		
		if(total != 0){
			listPage.setData(baseMapper.queryNormalGameByRectType(pageSize*(currentPage-1),pageSize,platform));
		}
		
		return listPage;
	}
}
